package leetcode;


import org.apache.spark.sql.sources.In;
import org.codehaus.jackson.map.util.LinkedNode;
import org.spark_project.jetty.util.ArrayQueue;
import scala.Int;
import sun.reflect.generics.tree.Tree;

import java.io.File;
import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//目的：生产一个，消费一个。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    ListNode(){}
}
class Solution {
    public ListNode detectCycle(ListNode head) {
        //遍历链表，使用Set集合存放链表节点地址
        HashSet<ListNode> list_set = new HashSet<>();
        while (head != null){
            if (list_set.contains(head)){
                return head;
            }
            list_set.add(head);
            head = head.next;
        }
        return null;
    }
}

public class Test {
    public static void main(String[] args) throws InterruptedException {
        int[] arr1 = new int[]{5,7,6,2,1};
        int[] arr2 = new int[]{1,7,3,6,2,1};
        ListNode headA = new ListNode();
        ListNode headB = new ListNode();

        addFunc(headA, arr1);
        addFunc(headB, arr2);

        System.out.println("headA------------------");
        //selFunc(headA);
        System.out.println("headB------------------");
        //selFunc(headB);

        //new Solution().detectCycle(headA, headB);

    }
    public static ListNode addFunc(ListNode head, int[] arr ){
        ListNode p = head;
        for (int i=0; i < arr.length; i++){
            p.val = arr[i];
            if (i == arr.length-1){
                p.next = null;
                break;
            }
            p.next = new ListNode();
            p = p.next;
        }
        return head;
    }
    public static void selFunc(ListNode head){

        while (head!=null){
            System.out.println(head.val);
            head = head.next;
        }
    }
}
